/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mx.im.sinocab.action;

import com.mx.im.sinocab.bean.Employ;
import com.mx.im.sinocab.dao.DaoEmploy;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tevez
 */
public class EmployAction extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");


            String redireccion = funcionInsertar(request, response);
            RequestDispatcher rd = getServletContext().getRequestDispatcher(redireccion);

            rd.forward(request, response);
        } catch (IllegalStateException e) {
            System.out.println("Error EmployAction: " + e.getMessage());

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }
 
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public static String funcionInsertar(HttpServletRequest request, HttpServletResponse response) {


        try {

            String name = request.getParameter("name");
            String lastName = request.getParameter("lastName");
            String phone = request.getParameter("phone");
            String cellphone = request.getParameter("cellphone");
            String address = request.getParameter("address");
            String gender = request.getParameter("gender");
            String birthday = request.getParameter("birthday").toString();
            String age = request.getParameter("age");
            String email = request.getParameter("email");
            String admissionDate = request.getParameter("admissionDate");
            String timeLaboring = request.getParameter("timeLaboring");
            String dependentEconomic = request.getParameter("dependentEconomic");
            String workedTipe = request.getParameter("workedTipe");
            String maritalStatus = request.getParameter("maritalStatus");
            String studies = request.getParameter("studies");

//        String photography = request.getParameter("photography");
//        String fingerPrint = request.getParameter("fingerPrint");

            Employ em = new Employ();

            em.setName(name);
            em.setLastName(lastName);
            em.setPhone(phone);
            em.setCellphone(cellphone);
            em.setAddress(address);
            em.setGender(gender);
            em.setBirthday(null);
            em.setAge(age);
            em.setEmail(email);
            em.setAdmissionDate(null);
            em.setTimeLaboring(timeLaboring);
            em.setDependentEconomic(dependentEconomic);
            em.setWorkedTipe(workedTipe);
            em.setMaritalStatus(maritalStatus);
            em.setStudies(studies);

//        em.setPhotography(photography);
//        em.setFingerPrint(fingerPrint);

            DaoEmploy dao = new DaoEmploy();


            boolean insertOK = dao.InsertarEmpleado(em);

            request.setAttribute("insertOK: ", insertOK);


        } catch (SQLException e) {

            System.out.println("Error insertOK: " + e.getMessage());
        }
        return "/Jsp/body/JspAdminEmploy.jsp";

    }

    public static boolean InsertaUsuario(Employ bean) {
        boolean reg = false;

        DaoEmploy dao = new DaoEmploy();
        try {

            reg = dao.InsertarEmpleado(bean);


            reg = dao.InsertarEmpleado(bean);
        } catch (Exception ex) {
            System.out.println("InsertarEmpleado: " + ex.getMessage());
        }

        return reg;
    }
}
